<?php

class CommonControl extends Control {

    protected function assign_head() {
        $this->top_cate();
        $this->_all_ask();
        $this->_top_ans();
        $this->_honor();
        if (isset($_SESSION['uid'])) {
            $this->nuser_info();
        }
    }

    //当前登陆用户信息
    public function nuser_info() {
        $this->user_info($_SESSION['uid'], 'userinfo', 'percent');
    }

    protected function _cate_info() {
        //分类信息
        if (isset($_GET['cid'])) {
            $pid = (int) $_GET['cid'];
            $data = M('cate')->query('select * from hd_category where cid=' . $_GET['cid']);
            if (empty($data)) {
                $pid = 0;
            }
            //获得当前cid的子分类
            $data2 = M('category')->query('SELECT *,c.cid as c FROM hd_category as c left join hd_ask as a on c.cid=a.cid where pid=' . $pid);
            if (empty($data2)) {
                $cid = M('category')->query('SELECT pid FROM hd_category where cid=' . $pid);
                $data2 = M('category')->query('SELECT *,c.cid as c FROM hd_category as c left join hd_ask as a on c.cid=a.cid where pid=' . $cid[0]['pid']);
                //如果没有子分类该采取的措施
            }
            //分配子分类
            $this->assign('cateinfo', $data2);
            $this->assign('cateid', $data2[0]);
            //抓取到所有分类信息
            $cate = M('category')->query('SELECT * FROM hd_category');
            $cate_list = $this->father_cate($cate, $pid);
            $cate_list = array_reverse($cate_list);
            $this->assign('cate_list', $cate_list);
        }
    }

    //递归获取到所有父级的分类信息
    protected function father_cate($cate, $pid) {
        $arr = array();
        foreach ($cate as $k => $v) {
            if ($pid == $v['cid']) {
                $arr[] = $v;
                $arr = array_merge($arr, $this->father_cate($cate, $v['pid']));
            }
        }
        return $arr;
    }

    //获取某个用户的信息的方法
    private function user_info($uid, $val, $val2) {
        $uid = (int) $uid;
        $data = M('category')->query('select * from hd_user where uid=' . $uid);

        if ($data[0]['answer']) {
            $percent = round($data[0]['accept'] / $data[0]['answer'], 2) * 100;
        } else {
            $percent = 0;
        }
        $this->assign($val, $data[0]);
        $this->assign($val2, $percent);
    }

    //问答之星
    private function _top_ans() {
        //获取本日回答问题最多的人
        $time = strtotime('00:00:01');
        $sql = 'SELECT count(*)  as c ,a.uid FROM hd_user as u left join hd_answer as a on u.uid=a.uid where  a.time>' . $time . ' group by a.uid order by c desc limit 1 ';
        $data = M('user')->query($sql);
        //获取历史回答问题最多的人
        $sql = 'SELECT count(*)  as c ,a.uid FROM hd_user as u join hd_answer as a on u.uid=a.uid  group by a.uid order by c desc limit 1 ';
        $data2 = M('user')->query($sql);
        if (!empty($data)) {
            $this->assign('topnum', $data[0]['c']);
            $uid = (int) $data[0]['uid'];
            $this->user_info($uid, 'topAns', 'percent2');
        }
        if (!empty($data2)) {
            $hisUid = (int) $data2[0]['uid'];
            $this->user_info($hisUid, 'HistoryAns', 'percent3');
        }
        //获得回答最多的人的UID
    }
    //回答光荣榜
    private function _honor() {
        $data = M('user')->query('SELECT * FROM hd_user where accept>0 order by accept desc limit 5');
        $this->assign('honor', $data);
    }

    private function _all_ask() {
        $data = M('category')->query('SELECT count(*)  as c  FROM hd_ask');
        $this->assign('count', $data[0]);
    }

    private function top_cate() {
        $data = M('category')->query('SELECT * FROM hd_category where pid=0');
        $this->assign('topCate', $data);
    }

}
